<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/12/16
 * Time: 12:46
 * 群发记录
 */
namespace app\send\controller;

use think\facade\View;
use think\facade\Request;
use app\admin\controller\AdminBase;

class SendQueue extends AdminBase
{
    public function index()
    {
        $page_no                = Request::param('page_no', 1);
        $search_filter          = Request::param('search_filter', array());
        $status                 = Request::param('status', 1);
        $send_task_id           = Request::param('send_task_id', 0);
        $progress               = Request::param('progress', 0);

        $filter = array();

        if (isset($search_filter['title']) && (trim($search_filter['title']) || trim($search_filter['title']) == "0")) {
            $search_filter['title'] = trim($search_filter['title']);
            $filter[] = ['title', 'like', '%'.$search_filter['title'].'%'];
        }

        if ($progress && $progress >= 1) {
            $real_progress = $progress-1;
            $filter[] = ['progress', '=', $real_progress];
        }

        if (isset($search_filter['start_date']) && $search_filter['start_date']) {
            $filter[] = ['add_time', '>=', $search_filter['start_date'].' 00:00:00'];
            if (isset($search_filter['start_time']) && $search_filter['start_time']) {
                $filter[] = ['add_time', '>=', $search_filter['start_date'].' '.$search_filter['start_time'].':00'];
            }
        }

        if ($send_task_id) {
            $filter[] = ['send_task_id', '=', $send_task_id];
        }

        if (isset($search_filter['stop_date']) && $search_filter['stop_date']) {
            $filter[] = ['add_time', '<=', $search_filter['stop_date'].' 23:59:59'];
            if (isset($search_filter['stop_time']) && $search_filter['stop_time']) {
                $filter[] = ['add_time', '<=', $search_filter['stop_date'].' '.$search_filter['stop_time'].':59'];
            }
        }

        $filter[] = ['status', '=', $status];

        $send_queue_list = array();

        $send_queue_list = getList('send_queue', $filter, ['id', 'desc']);
//        foreach ($send_queue_list as $k=>$v) {
//            $store_target = $v['store_target'];
//            // 重新拆分
//            if (strpos($store_target, ',') !== false) {
//                $store_target = explode(',', trim($store_target, ','));
//            }
//
//            $send_queue_list[$k]['user_num'] = user_helper::get_user_num_by_filter(array('store_id'=>$store_target, 'is_subscribe'=>1));
//        }

        return View::fetch('send_queue', [
            'progress' => $progress,
            'search_filter'=> $search_filter,
            'status'=> $status,
            'send_task_id'=> $send_task_id,
            'list'=> $send_queue_list
        ]);
    }

}

